﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gma.UserActivityMonitor;

namespace MercuryFE
{
    public partial class KeyMapper : Form
    {

        private Dictionary<string, string> _keyMappings = null;
        private DataTable _mappings = null;

        public string KeyMappingString = null;

        public KeyMapper(string keyMappingString)
        {
            InitializeComponent();
            this._keyMappings = LauncherLib.StringToKeyMapping(keyMappingString);
            this.KeyMappingString = keyMappingString;
            this.Load += new EventHandler(KeyMapper_Load);
        }

        void KeyMapper_Load(object sender, EventArgs e)
        {
            _mappings = new DataTable();
            _mappings.Columns.Add("Original", typeof(string));
            _mappings.Columns.Add("New", typeof(string));
            // create dataset from mappings
            if (_keyMappings != null && _keyMappings.Count > 0)
            {
                foreach (KeyValuePair<string, string> mapping in _keyMappings)
                {
                    _mappings.Rows.Add(mapping.Key, mapping.Value);
                }
            }

            dgvKeyMappings.DataSource = _mappings;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string origCode = tbOriginalKeyCode.Text;
            string newCode = tbNewKeyCode.Text; // (cbSuppress.Checked ? null : tbNewKeyCode.Text);

            if(cbSuppress.Checked)
                _mappings.Rows.Add(origCode, null);
            else
                _mappings.Rows.Add(origCode, newCode);

            tbOriginalKeyCode.Text = "";
            tbNewKeyCode.Text = "";
            cbSuppress.Checked = false;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Dictionary<string,string> tempKeyMappings = new Dictionary<string, string>();

            foreach (DataRow dr in _mappings.Rows)
            {
                
                string newString = dr["New"].ToString();
                if (newString.Trim() == "")
                    newString = null;
                tempKeyMappings.Add(dr["Original"].ToString(), newString);
            }
            this.KeyMappingString = LauncherLib.KeyMappingToString(tempKeyMappings);
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            // send back null if canceled
            //this.KeyMappingString = null;
            this.Close();
        }


        private void cbSuppress_CheckedChanged(object sender, EventArgs e)
        {
            if (cbSuppress.Checked)
            {
                tbNewKeyCode.Text = "";
                tbNewKeyCode.Enabled = false;
            }
            else
            {
                tbNewKeyCode.Enabled = true;
            }

        }


        private void tbOriginalKeyCode_Enter(object sender, EventArgs e)
        {
            HookManager.KeyUp += new KeyEventHandler(tbOriginalKeyCode_KeyUp);
        }

        private void tbOriginalKeyCode_Leave(object sender, EventArgs e)
        {
            HookManager.KeyUp -= new KeyEventHandler(tbOriginalKeyCode_KeyUp);
        }

        void tbOriginalKeyCode_KeyUp(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;
            tbOriginalKeyCode.Text = e.KeyCode.ToString();
        }



        private void tbNewKeyCode_Enter(object sender, EventArgs e)
        {
            HookManager.KeyDown += new KeyEventHandler(tbNewKeyCode_KeyUp);
        }

        private void tbNewKeyCode_Leave(object sender, EventArgs e)
        {
            HookManager.KeyDown -= new KeyEventHandler(tbNewKeyCode_KeyUp);
        }

        void tbNewKeyCode_KeyUp(object sender, KeyEventArgs e)
        {
           e.SuppressKeyPress = true;
           tbNewKeyCode.Text = e.KeyCode.ToString();
        }
    }
}
